package pro1.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import pro1.model.Module;

public interface ModuleMapper {
	int deleteByPrimaryKey(Integer id);

	int insert(Module record);

	int insertSelective(Module record);

	Module selectByPrimaryKey(Integer id);

	int updateByPrimaryKeySelective(Module record);

	int updateByPrimaryKey(Module record);

	@Select("select id, name, path, info, createtime from module order by createtime desc")
	List<Module> selectAll();

	@Select("select m.id, m.name, m.path, m.info, m.createtime, COALESCE(c.id,0) as has from module as m left join character_module as c on m.id=c.moduleid and c.characterid=#{chaId} order by m.createtime desc;")
	List<Map<String, Object>> selectAllByCharacterId(@Param("chaId") Integer chaId);

	@Select("select id, name, path, info, createtime from module where name=#{name}")
	Module selectByName(@Param("name") String name);

	@Select("select id, name, path, info, createtime from module where path=#{path}")
	Module selectByPath(@Param("path") String path);

	@Select("select COALESCE(count(id),0) from module where path=#{path} and id in (select moduleid from character_module "
			+ "where characterid in(select characterid from admin_character where id=#{userId}))")
	int selectByUserIdAndPath(@Param("userId") Integer userId, @Param("path") String path);
}